package com.polaris.live.dialog

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.polaris.live.R
import com.polaris.live.adapter.live.AdministratorUserAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.DialogAdministratorListBinding
import com.polaris.live.dialog.common.ConfirmCanalDialog
import com.polaris.live.net.liveService
import com.polaris.live.resp.back_resp.LiveAdministratorResp
import com.polaris.live.translation.R.string
import com.polaris.live.widget.refresh.EmptyView

/**
 * 管理员列表
 *
 * @author Created by 半仙 on 2024/5/30/030 11:40
 */
class AdministratorDialog(
    context: Context,
    private val mViewModel: BaseViewModel,
    private val mLiveAdministratorResp: LiveAdministratorResp,
) : BottomPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_administrator_list
    }

    private lateinit var mBinding: DialogAdministratorListBinding

    private val mAdapter by lazy { AdministratorUserAdapter() }

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogAdministratorListBinding.bind(popupImplView)
        mBinding.tvUserNum.text = "(${mLiveAdministratorResp.user.size}/${mLiveAdministratorResp.maxCount})"
        mBinding.ivClose.setOnSingleClickListener {
            dismiss()
        }
        mBinding.userRecycler.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(context)
            itemAnimator = null
        }

        mAdapter.setList(mLiveAdministratorResp.user)
        if (CollectionUtils.isEmpty(mLiveAdministratorResp.user)) {
            mAdapter.setEmptyView(EmptyView(context))
        }
        mAdapter.setOnItemChildClickListener { _, view, position ->
            val item = mAdapter.getItemOrNull(position) ?: return@setOnItemChildClickListener
            when (view.id) {
                R.id.tv_remove_user -> {
                    val mTitle = StringUtils.getString(string.are_you_sure_remove_user, item.nickname)
                    XPopup.Builder(context)
                        .hasShadowBg(false)
                        .isDestroyOnDismiss(true)
                        .asCustom(ConfirmCanalDialog(context, mTitle, onConfirmCallback = {
                            removeUser(position, item.userId)
                        })).show()
                }
            }
        }

        mBinding.ivIllustrate.setOnSingleClickListener {
            XPopup.Builder(context)
                .hasShadowBg(false)
                .isDestroyOnDismiss(true)
                .asCustom(AdministratorIllustrateDialog(context, mLiveAdministratorResp.maxCount))
                .show()
        }

        NavigationBarUtils.setNavigationHeightIfTransparent(mBinding.vLine) { view, height ->
            val layoutParams = view.layoutParams as ViewGroup.LayoutParams
            layoutParams.height = height
            mBinding.vLine.layoutParams = layoutParams
        }
    }

    private fun removeUser(position: Int, userId: Long) {
        mViewModel.requestLoading({ liveService.administratorRemove(userId) }, {
            mAdapter.removeAt(position)
            mBinding.tvUserNum.text = "(${mLiveAdministratorResp.user.size}/${mLiveAdministratorResp.maxCount})"
        }, {
            ToastGravity.showGravity(it.errorMsg)
        }, waitTime = 0)
    }
}